package com.gange;

import com.gange.proxy.AnjukeProxy;
import com.gange.proxy.Fangdong;
import com.gange.proxy.Zufang;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyMain {
//    public static void main(String[] args) {
//        Zufang proxy = new AnjukeProxy();
//        proxy.chuzu();
//    }
//    public static void main(String[] args) {
//
//        Zufang zufang = (Zufang) Proxy.newProxyInstance(Fangdong.class.getClassLoader(), Fangdong.class.getInterfaces(), new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                Fangdong fd = new Fangdong();
//                //额外功能
//                System.out.println("---------我是额外功能------------");
//                return method.invoke(fd, args);
//            }
//        });
//
//        zufang.chuzu();
//    }
    public static void main(String[] args) {
        //cglib的动态代理，目标类可以没有实现接口
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Fangdong.class);
        enhancer.setCallback(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Fangdong fd = new Fangdong();
                //额外功能
                System.out.println("---------我是额外功能------------");
                return method.invoke(fd, args);
            }
        });
        Fangdong fd = (Fangdong) enhancer.create();
        System.out.println(fd.getClass());
        fd.chuzu();
    }
}
